package com.deyuanyun.pic.ctrl.service.pushmsg;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspOcgradeExcel;
import com.deyuanyun.pic.settings.controller.dto.BizMsgThresholdVO;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.domain.base.BizMsgUser;
import com.deyuanyun.pic.settings.enumeration.ThresholdType;
import com.deyuanyun.pic.settings.service.msgtemplate.AbstractPushMsgTemplate;
import com.deyuanyun.pic.settings.service.msgtemplate.MsgQueue;
import com.deyuanyun.pic.settings.service.pipe.PipeLocationService;
import com.deyuanyun.pic.settings.support.utils.PipeDictUtils;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *  腐蚀控制---防腐层等级结果录入消息推送
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-10-24
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
public class ExinspOcgradePushMsg extends AbstractPushMsgTemplate {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExinspInsedJointSaalAnodePushMsg.class);

    @Override
    public void doPushMsgData() {
        if(super.interfaceParam.isEmpty()) {
            LOGGER.info("没有传入参数");
            return;
        }
        BizMsgThresholdVO msgThresholdVO = super.bizMsgThresholdService.queryDataByTypeOne(ThresholdType.INTERNAL_PROTECTING_COATING_INPUT.getValue());

        Object objThre = msgThresholdVO.getThresholdContentJson();
        if(objThre == null || objThre == "" || !(objThre instanceof Map)) {
            LOGGER.info("没有设置对应的字段值或者不是map结构");
            return;
        }
        Map thresholdMap = (Map) objThre;

        boolean isThree = false, isFour = false;
        Object threeObj = thresholdMap.get("three");
        Object fourObj = thresholdMap.get("four");

        if(threeObj != null && !"".equals(threeObj)) //三级
            isThree = true;
        if(fourObj != null && !"".equals(fourObj)) //四级
            isFour = true;

        if(isThree || isFour) {
            Object obj = super.interfaceParam.get(CtrlPushMsgCons.LIST_KEY);

            if(obj != null && (obj instanceof List)) {
                List<PicCtrlExinspOcgradeExcel> list = (List) obj;
                for (int i = 0; i < list.size(); i++) {
                    PicCtrlExinspOcgradeExcel ocgradeExcel = list.get(i);
                    String str = "";
                    if("3".equals(ocgradeExcel.getCoatingGrade()) && isThree) {
                        str = "三级";
                    }
                    if("4".equals(ocgradeExcel.getCoatingGrade()) && isFour) {
                        str = "四级";
                    }
                    if(StringUtils.isNotEmpty(str)) {
                        initializeBizMsgSends(msgThresholdVO, ocgradeExcel.getPipeLineId(),
                                ocgradeExcel.getBeginStationNumber(), ocgradeExcel.getEndStationNumber(), str);
                    }
                }
            }
        }
    }

    /**
     * @param args 参数说明：
     * <pre>
     * arg[0]：为业务阀值实体信息，类型为：{@link BizMsgThresholdVO}<br />
     * arg[1]：为管道id，类型为：{@link String}
     * arg[2]: 为起点测试桩位置id，类型为：{@link String}
     * arg[3]: 为终点测试桩位置id，类型为：{@link String}
     * arg[4]: 为当前等级，类型为：{@link String}
     * </pre>
     */
    @Override
    public void initializeBizMsgSends(Object... args) {
        BizMsgThresholdVO msgThresholdVO = (BizMsgThresholdVO) args[0];
        String pipeLineId = (String) args[1];
        String fromId = (String) args[2];
        String toId = (String) args[3];
        String str = (String) args[4];

        super.roleList = msgThresholdVO.getRoleIdList();
        if ( super.roleList != null && ! super.roleList.isEmpty()) {
            if (StringUtils.isNotEmpty(pipeLineId) && StringUtils.isNotEmpty(fromId) && StringUtils.isNotEmpty(toId)) {
                String pipeLineName = PipeDictUtils.getLineById(pipeLineId).getName();
                PipeLocationService pipeLocationService = SpringAppliction.getBean(PipeLocationService.class);
                PipeLocationDTO fromDTO = pipeLocationService.queryPipeLocationById(pipeLineId, fromId);
                PipeLocationDTO toDTO = pipeLocationService.queryPipeLocationById(pipeLineId, toId);
                if (fromDTO != null && toDTO != null) {
                    super.thresListCastString(Lists.newArrayList(pipeLineName, fromDTO.getName(), toDTO.getName(), str));

                    Set<String> userIds = super.bizMsgThresholdService.queryUseIdByRoleIds( super.roleList); //用户id集合
                    List<MsgQueue.BizMsgUserInfo> userInfos = new ArrayList<MsgQueue.BizMsgUserInfo>();

                    String msgContent = msgThresholdVO.getMsgContent();
                    for (String userId : userIds) {
                        BizMsgUser bizMsgUser = new BizMsgUser();
                        if (StringUtils.isNotEmpty(msgContent))
                            bizMsgUser.setMsgContent(msgContent);
                        if (StringUtils.isNotEmpty(super.thresholdContent)) { //设置阀值内容
                            bizMsgUser.setThresholdContent(super.thresholdContent);
                        }
                        bizMsgUser.setCreateRemark("防腐层评价结果录入消息推送");
                        bizMsgUser.setCreateDate(new Date());
                        bizMsgUser.setUserId(userId);
                        bizMsgUser.setId(CommonUtil.getUUID());

                        userInfos.add(msgQueue.new BizMsgUserInfo().setValid(false).setBizMsgUser(bizMsgUser));
                    }
                    if(!userInfos.isEmpty()) {
                        //封装好需要发送的消息信息交给队列来推送消息
                        MsgQueue.MsgInfo msgInfo = msgQueue.new MsgInfo().setBizMsgThresholdId(msgThresholdVO.getId())
                                .setThresholdSet(msgThresholdVO.getThresholdContent()).setRoleIds(super.roleList);
                        msgQueue.addOperator(msgQueue.new PushMsgOperator(userInfos, msgInfo));
                    }
                }
            }
        }
    }

    @Override
    public boolean valiIsPush(Map<String, Object> valiMap) {
        return false;
    }
}
